/**
 * 递归生成树
 */
export const recursionTrees = (items: any, id = 'id', parentId = 'parentId',  rootId = 0) => {
    const result = [];
    for (let i = 0; i < items.length; i++) {
        if (items[i][parentId] === rootId) {
            const children = recursionTrees(items, id, parentId, items[i][id]);
            if (children.length) {
                items[i].children = children;
            }
            result.push(items[i]);
        }
    }
    return result;
}

/**
 * @param { Boolean } type 格式 [true：2023年11月27日，false：2023-11-27]
 * @param { Boolean } hasWeek 是显示星期 [true：显示，false：不显示]
 * @param { Boolean } hasTime 是否时分秒 [true：显示，false：不显示]
 */
export const getCurrentDate = (type: boolean, hasWeek: boolean, hasTime: boolean) => {
    var date = new Date();
    var year = singular(date.getFullYear());
    var month = singular(date.getMonth() + 1);
    var dates = singular(date.getDate());
    var day = date.getDay();
    var hours = singular(date.getHours());
    var minutes = singular(date.getMinutes());
    var seconds = singular(date.getSeconds());
    var _timeStr = '';
    if (type) {
        _timeStr = `${year}年${month}月${dates}日 `;
    } else {
        _timeStr = `${year}-${month}-${dates}日`;
    }
    if (hasWeek) {
        let _week = '';
        switch (day) {
            case 0:
                _week = '星期日 ';
                break;
            case 1:
                _week = '星期一 ';
                break;
            case 2:
                _week = '星期二 ';
                break;
            case 3:
                _week = '星期三 ';
                break;
            case 4:
                _week = '星期四 ';
                break;
            case 5:
                _week = '星期五 ';
                break;
            case 6:
                _week = '星期六 ';
                break;
        }
        _timeStr += _week;
    }
    if (hasTime) {
        _timeStr += `${hours}:${minutes}:${seconds}`;
    }
    return _timeStr;
}

/**
 * 单数补位
 */
export const singular = (num: number) => {
    let _num:string = ''+ num;
    if (num < 10) {
        _num = '0' + num;
    }
    return _num;
}

/**
 * 根据当前时间设置显示文本
 */
export const getCurrentTimeText = () => {
    const date = new Date();
    const hours = date.getHours();
    let timeText = '';
    if (hours >= 0 && hours < 12) {
        timeText = '早上好';
    } else if (hours >= 12 && hours <= 14) {
        timeText = '中午好';
    } else if (hours > 14&& hours <= 18) {
        timeText = '下午好';
    } else {
        timeText = '晚上好';
    } 
    return timeText;
}